home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1994 November / macformat-018.iso / Utility Spectacular / Compression / AutoBin w⁄src / DSUtils.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-19  |  8.0 KB  |  276 lines  |  [TEXT/MPS ]

  1. /******************************************************************************
  2. **
  3. **  Project Name:    DropShell
  4. **     File Name:    DSUtils.c
  5. **
  6. **   Description:    Utility routines that may be useful to DropBoxes
  7. **
  8. *******************************************************************************
  9. **                       A U T H O R   I D E N T I T Y
  10. *******************************************************************************
  11. **
  12. **    Initials    Name
  13. **    --------    -----------------------------------------------
  14. **    LDR            Leonard Rosenthol
  15. **
  16. *******************************************************************************
  17. **                      R E V I S I O N   H I S T O R Y
  18. *******************************************************************************
  19. **
  20. **      Date        Time    Author    Description
  21. **    --------    -----    ------    ---------------------------------------------
  22. **    12/09/91            LDR        Added the Apple event routines
  23. **    11/24/91            LDR        Original Version
  24. **
  25. ******************************************************************************/
  26.  
  27. #include "DSGlobals.h"
  28. #include "DSUtils.h"
  29.  
  30. /*
  31.     This routine is used to properly center an Alert before showing.
  32.     
  33.     It is per Human Interface specs by putting it in the top 1/3 of screen.
  34.     NOTE: This same technique can be used with DLOG resources as well.
  35. */
  36. #pragma segment Main
  37. void CenterAlert ( short theID ) {
  38.     short        theX, theY;
  39.     AlertTHndl    theAlertHandle;
  40.     Rect        screen, alrt;
  41.     
  42.     theAlertHandle = (AlertTHndl) GetResource ( 'ALRT', theID );
  43.     if ( theAlertHandle != NULL ) {
  44.         HLock ((Handle) theAlertHandle );
  45.  
  46.         alrt = (*theAlertHandle)->boundsRect;
  47.         screen = qd.screenBits.bounds;
  48.         
  49.         theX = (( screen.right - screen.left ) - (alrt.right - alrt.left )) >> 1;
  50.         theY = (( screen.bottom - screen.top ) + GetMBarHeight () - (alrt.bottom - alrt.top)) >> 1;
  51.         theY -= ( screen.bottom - screen.top ) >> 2;    /* this moves it up for better viewing! */
  52.         OffsetRect ( &(*theAlertHandle)->boundsRect, theX - alrt.left, theY - alrt.top );
  53.     }
  54.         
  55.     SetCursor ( &qd.arrow );    // change this for code resources!
  56. }
  57.  
  58. /*
  59.     This routine is just a quick & dirty error reporter
  60. */
  61. #pragma segment Main
  62. void ErrorAlert ( short stringListID, short stringIndexID, short errorID ) {
  63.     #define    kAlertID    200
  64.     Str255    param, errorStr;
  65.  
  66.     NumToString ( errorID, errorStr );
  67.     GetIndString ( param, stringListID, stringIndexID );
  68.     ParamText ( param,  errorStr, NULL, NULL );
  69.     CenterAlert ( kAlertID );
  70.     (void) Alert ( kAlertID, NULL );
  71. }
  72.  
  73. /*** These routines use the Process Manager to give you information about yourself ***/
  74.  
  75. #pragma segment Main
  76. void GetAppName(Str255 appName)    {
  77.     OSErr                err;
  78.     ProcessInfoRec        info;
  79.     ProcessSerialNumber    curPSN;
  80.  
  81.     err = GetCurrentProcess(&curPSN);
  82.     
  83.     info.processInfoLength = sizeof(ProcessInfoRec);    // ALWAYS USE sizeof!
  84.     info.processName = appName;                            // so it returned somewhere
  85.     info.processAppSpec = NULL;                            // I don't care!
  86.  
  87.     err = GetProcessInformation(&curPSN, &info);
  88. }
  89.  
  90. #pragma segment Main
  91. void GetAppFSSpec(FSSpec *appSpec)    {
  92.     OSErr                err;
  93.     Str255                appName;
  94.     ProcessInfoRec        info;
  95.     ProcessSerialNumber    curPSN;
  96.  
  97.     err = GetCurrentProcess(&curPSN);
  98.     
  99.     info.processInfoLength = sizeof(ProcessInfoRec);    // ALWAYS USE sizeof!
  100.     info.processName = appName;                            // so it returned somewhere
  101.     info.processAppSpec = appSpec;                        // so it can get returned!
  102.  
  103.     err = GetProcessInformation(&curPSN, &info);
  104. }
  105.  
  106.  
  107. /* ••• Apple event routines begin here ••• */
  108.  
  109. /*
  110.     This routine will create a targetDesc for sending to self.
  111.  
  112.     We take IM VI's advice and use the typePSN form with 
  113.     kCurrentProcess as the targetPSN.
  114. */
  115. OSErr GetTargetFromSelf (AEAddressDesc *targetDesc)
  116. {
  117.     ProcessSerialNumber    psn;
  118.  
  119.     psn.highLongOfPSN     = 0;
  120.     psn.lowLongOfPSN     = kCurrentProcess;
  121.  
  122.     return( AECreateDesc(typeProcessSerialNumber, (Ptr)&psn, sizeof(ProcessSerialNumber), targetDesc) );
  123. }
  124.  
  125. /* This routine will create a targetDesc using the apps signature */
  126. OSErr GetTargetFromSignature (OSType processSig, AEAddressDesc *targetDesc)
  127. {
  128.     return( AECreateDesc(typeApplSignature, (Ptr)&processSig, sizeof(processSig), targetDesc) );
  129. }
  130.  
  131. /* This routine will create a targetDesc by bringing up the PPCBrowser */
  132. OSErr GetTargetFromBrowser(Str255 promptStr, AEAddressDesc *targetDesc)
  133. {
  134.     OSErr        err;
  135.     TargetID    theTarget;
  136.     PortInfoRec    portInfo;
  137.     
  138.     err = PPCBrowser(promptStr, "\p", false, &theTarget.location, &portInfo, NULL, "\p");
  139.     if (err == noErr) {
  140.         theTarget.name = portInfo.name;
  141.         err = AECreateDesc(typeTargetID, (Ptr)&theTarget, sizeof(TargetID), targetDesc);
  142.     }
  143.     return( err );
  144. }
  145.  
  146.  
  147. /*
  148.     This routine is the low level routine used by the SendODOCToSelf
  149.     routine.  It gets passed the list of files (in an AEDescList)
  150.     to be sent as the data for the 'odoc', builds up the event
  151.     and sends off the event.  
  152.  
  153.     It is broken out from SendODOCToSelf so that a SendODOCListToSelf could
  154.     easily be written and it could then call this routine - but that is left
  155.     as an exercise to the reader.
  156.     
  157.     Read the comments in the code for the order and details
  158. */
  159. void _SendDocsToSelf (AEDescList *aliasList)
  160. {
  161.     OSErr            err;
  162.     AEAddressDesc    theTarget;
  163.     AppleEvent        openDocAE, replyAE;
  164.  
  165. /*
  166.     First we create the target for the event.   We call another
  167.     utility routine for creating the target.
  168. */
  169.     err = GetTargetFromSelf(&theTarget);
  170.     if (err == noErr) {
  171.         /* Next we create the Apple event that will later get sent. */
  172.         err = AECreateAppleEvent(kCoreEventClass, kAEOpenDocuments, &theTarget, kAutoGenerateReturnID, kAnyTransactionID, &openDocAE);
  173.  
  174.         if (err == noErr) {
  175.             /* Now add the aliasDescList to the openDocAE */
  176.             err = AEPutParamDesc(&openDocAE, keyDirectObject, aliasList);
  177.  
  178.             if (err == noErr) {
  179.                 /*
  180.                     and finally send the event
  181.                     Since we are sending to ourselves, no need for reply.
  182.                 */
  183.                 err = AESend(&openDocAE, &replyAE, kAENoReply + kAECanInteract, kAENormalPriority, 3600, NULL, NULL);
  184.  
  185.                 /*
  186.                     NOTE: Since we are not requesting a reply, we do not need to
  187.                     need to dispose of the replyAE.  It is there simply as a 
  188.                     placeholder.
  189.                 */
  190.             }
  191.  
  192.         /*    
  193.             Dispose of the aliasList descriptor
  194.             We do this instead of the caller since it needs to be done
  195.             before disposing the AEVT
  196.         */
  197.             err = AEDisposeDesc(aliasList);
  198.         }
  199.  
  200.     /*and of course dispose of the openDoc AEVT itself*/
  201.         err = AEDisposeDesc(&openDocAE);
  202.     }
  203. }
  204.  
  205. /*
  206.     This is the routine called by SelectFile to send a single odoc to ourselves.
  207.     
  208.     It calls the above low level routine to do the dirty work of sending the AEVT -
  209.     all we do here is build a AEDescList of the file to be opened.
  210. */
  211. void SendODOCToSelf (FSSpec *theFileSpec) {
  212.  
  213.     OSErr        err;
  214.     AEDescList    aliasList;
  215.     AEDesc        aliasDesc;
  216.     AliasHandle    aliasH;
  217.     
  218.     /*Create the descList to hold the list of files*/
  219.     err = AECreateList(NULL, 0, false, &aliasList);
  220.  
  221.     if (err == noErr) {
  222.         /* First we setup the type of descriptor */
  223.         aliasDesc.descriptorType = typeAlias;
  224.  
  225.         /*
  226.             Now we add the file to descList by creating an alias and then
  227.             adding it into the descList using AEPutDesc
  228.         */
  229.         err = NewAlias(NULL, theFileSpec, &aliasH);
  230.         aliasDesc.dataHandle = (Handle)aliasH;
  231.         err = AEPutDesc(&aliasList, 0, &aliasDesc);
  232.         DisposHandle((Handle)aliasH);
  233.  
  234.         /*Now call the real gut level routine to do the dirty work*/
  235.         _SendDocsToSelf(&aliasList);
  236.  
  237.         /*_SendDocsToSelf will dispose of aliasList for me*/
  238.     }
  239. }
  240.  
  241.  
  242. void SendQuitToSelf (void)
  243. {
  244.     OSErr            err;
  245.     AEAddressDesc    theTarget;
  246.     AppleEvent        quitAE, replyAE;
  247.  
  248. /*
  249.     First we create the target for the event.   We call another
  250.     utility routine for creating the target.
  251. */
  252.     err = GetTargetFromSelf(&theTarget);
  253.     if (err == noErr) {
  254.         /* Next we create the Apple event that will later get sent. */
  255.         err = AECreateAppleEvent(kCoreEventClass, kAEQuitApplication, &theTarget, kAutoGenerateReturnID, kAnyTransactionID, &quitAE);
  256.  
  257.         if (err == noErr) {
  258.             /*
  259.                 and finally send the event
  260.                 Since we are sending to ourselves, no need for reply.
  261.             */
  262.             err = AESend(&quitAE, &replyAE, kAENoReply + kAECanInteract, kAENormalPriority, 3600, NULL, NULL);
  263.  
  264.             /*
  265.                 NOTE: Since we are not requesting a reply, we do not need to
  266.                 need to dispose of the replyAE.  It is there simply as a 
  267.                 placeholder.
  268.             */
  269.         }
  270.  
  271.         /* and of course dispose of the quit AEVT itself */
  272.         err = AEDisposeDesc(&quitAE);
  273.     }
  274. }
  275.  
  276.